class RunnerController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, CourseService, RunnerService, getCourseList, iRunner){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
    	vm.$timeout 	 = $timeout;
    	vm.$state   	 = $state;
    	vm.ngDialog 	 = ngDialog;
    	vm.dialogService = dialogService;
        vm.CourseService = CourseService;
    	vm.RunnerService = RunnerService;
        vm.getCourseList = getCourseList;
    	vm.iRunner 	     = iRunner;

        var phone_exp    = /^\d{11}$/,
            identity_exp = /^[A-Za-z0-9]+$/,
            email_exp    = /^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;

    	// 科目列表
        vm.getCourseList.code==0&&(vm.courselist = vm.getCourseList.data);
        
    	// 表单数据初始化
        vm.runner = {
            runner_num: '',
            runner_name: '',
            runner_gender: 'male',
            runner_phone: '',
            runner_identity: '',
            runner_email: '',
            runner_address: '',
            runner_course: '',
            team: ''
        }

        // 监听科目变化, 显示对应团队列表
        $scope.$watch(angular.bind(vm, () => vm.runner.runner_course), (newValue, oldValue) => {
            if (newValue) {
                vm.CourseService.getCourseTeam(vm.$cookieStore.get('access_token'), newValue.course_id)
                .then( data => {

                    data.code==0&&(vm.teamlist = data.data);
                    console.log(data)

                }, error => console.log('error'))
            } else if (newValue==undefined) {
                vm.teamlist = [];
            }
        });

        // 表单提交
        vm.submitForm = function(isValid) {

            // 验证失败, 提示用户
            if (!isValid) {
                
                if (!vm.runner.runner_course) {
                    $.blink(angular.element(document.getElementsByName('runnerCourse')), 5, 100)
                } else if (!vm.runner.runner_num) {
                    $.blink(angular.element(document.getElementsByName('runnerNum')), 5, 100)
                } else if (!vm.runner.runner_name) {
                    $.blink(angular.element(document.getElementsByName('runnerName')), 5, 100)
                } 

            }

            // 验证通过
            if (isValid) {
                
                if (vm.runner.runner_phone&&!phone_exp.test(vm.runner.runner_phone)) {
                    $.blink(angular.element(document.getElementsByName('runnerPhone')), 5, 100)
                    return false;
                } else if (vm.runner.runner_identity&&!identity_exp.test(vm.runner.runner_identity)) {
                    $.blink(angular.element(document.getElementsByName('runnerIdentity')), 5, 100)
                    return false;
                } else if (vm.runner.runner_email&&!email_exp.test(vm.runner.runner_email)) {
                    $.blink(angular.element(document.getElementsByName('runnerEmail')), 5, 100)
                    return false;
                }

                // 拼接POST数据格式
                var params = {
                    course_id: vm.runner.runner_course.course_id,
                    runner_name: vm.runner.runner_name,
                    runner_cell: vm.runner.runner_phone,
                    runner_mail: vm.runner.runner_email,
                    address: vm.runner.runner_address,
                    bib: vm.runner.runner_num,
                    runner_gender: vm.runner.runner_gender,
                    identity: vm.runner.runner_identity,
                    team_id: vm.runner.team ? vm.runner.team.team_id : ''
                }

                console.log(params)

                // 创建选手
                vm.RunnerService.postRunnerCreate(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                .then(function(data) {

                    // 成功调用
                    data.code==0&&vm.dialogService.success('创建成功', 'dashboard.race.runnerlist');

                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, function(data) {
                    console.log('error')
                });
            }

            return false;
        }

    }

}

RunnerController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'CourseService', 'RunnerService', 'getCourseList', 'iRunner']; 

export default RunnerController;